<?php 
    if (!isset($_SESSION))
    {
        session_start();
    }
    $_SESSION['gate'] = 1;
    trackAnonymousUserVisit();
	if ($_POST)
	{
		require('config.php');
		$username = $_POST["username"];
		$password = $_POST["password"];
		$md5 = md5($password);
		$stmt = $connection->prepare("select id from user_id_passwd where email=? and md5=?");
		$stmt->bind_param("ss", $username, $md5);
		$stmt->execute();
		$stmt->store_result();
		$row_cnt = $stmt->num_rows;
		//success login
		if ($row_cnt > 0)
		{
            //echo $username . $password . $md5;
            $_SESSION['username'] = $username;
			$stmt->bind_result($id);
			$stmt->fetch();
			$stmt = $connection->prepare('update user_id_passwd set cookie=1 where email=?');
			$stmt->bind_param("s", $username);
			$stmt->execute();
            $_SESSION['login'] = true;
            trackRegistedUserVisit($id);
			if (isset($_POST['stay']))
			{
				//save 30 days
				setcookie("userid", $id, time()+30*24*60*60);
				setcookie("usermd5", $md5, time()+30*24*60*60);
				header("location: home.php");
			}
			else
			{
				setcookie("userid", $id); 
				setcookie("usermd5", $md5);
				header("location: home.php");
            }
		}
		else
		{
            //echo $username . $password . $md5;
			header("location: anonymous.php");
		}
	}
	else
	{
        if ($_GET['fav_type'])
        {
            $_SESSION['fav_type'] = $_GET['fav_type'];
        }
        else
        {
            $_SESSION['fav_type'] = "";
        }
        if ($_SESSION['login'] == true)
        {
            trackRegistedUserVisit($_COOKIE['userid']);
            header("location: home.php");
        }
        else if (isset($_COOKIE['userid']) && isset($_COOKIE['usermd5']))
		{
			require('config.php');
			$stmt = $connection->prepare("select email from user_id_passwd where id=? and md5=? and cookie=1");
			$stmt->bind_param("ss", $_COOKIE['userid'], $_COOKIE['usermd5']);
			$stmt->execute();
            $stmt->bind_result($email);
			$stmt->store_result();
            if ($stmt->fetch()) 
            {
                $_SESSION['username'] = $email;
                $_SESSION['login'] = true;
                trackRegistedUserVisit($_COOKIE['userid']);
				header("location: home.php");
			}
			else
			{
				header("location: anonymous.php");
			}
		}
		else
		{
			header("location: anonymous.php");
		}
	}

    function trackAnonymousUserVisit()
    {
        $ip = getuserip();
	    $time = date("y-m-d h:i:s");
        $browser = $_SERVER["HTTP_USER_AGENT"];
		require('config.php');
        $stmt = $connection->prepare('insert into anonymous_user_visit (ip, time, browser) values (?, ?, ?)');
        $stmt->bind_param("sss", $ip, $time, $browser);
        $stmt->execute();
    }

    function trackRegistedUserVisit($user_id)
    {
	    $time = date("y-m-d h:i:s");
        $browser = $_SERVER["HTTP_USER_AGENT"];
		require('config.php');
        $stmt = $connection->prepare('insert into registed_user_visit (user_id, time, browser) values (?, ?, ?)');
        $stmt->bind_param("sss", $user_id, $time, $browser);
        $stmt->execute();
    }

    function getUserIP()
    {
        if(!empty($_SERVER["HTTP_CLIENT_IP"]))  
            $cip = $_SERVER["HTTP_CLIENT_IP"];  
        else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))  
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
        else if(!empty($_SERVER["REMOTE_ADDR"]))  
            $cip = $_SERVER["REMOTE_ADDR"];  
        else  
            $cip = "无法获取！";  
        return $cip;  
    }
?> 
